package com.scwatch.core.service.impl;

import com.scwatch.common.orm.Limitable;
import com.scwatch.common.orm.RowSide;
import com.scwatch.common.orm.SearchFilter;
import com.scwatch.core.domain.Program;
import com.scwatch.core.domain.User;
import com.scwatch.core.repository.ProgramDao;
import com.scwatch.core.service.ProgramService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by rh006 on 2017/7/21.
 */

@Service
@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
public class ProgramServiceImpl implements ProgramService{
    public Page<Program> findAll(Map<String, String[]> params, Pageable pageable) {
        return dao.findAll(spec(params), pageable);
    }

    public RowSide<Program> findSide(Map<String, String[]> params, Program bean, Integer position,
                                    Sort sort) {
        if (position == null) {
            return new RowSide<Program>();
        }
        Limitable limit = RowSide.limitable(position, sort);
        List<Program> list = dao.findAll(spec(params), limit);
        return RowSide.create(list, bean);
    }

    private Specification<Program> spec(Map<String, String[]> params) {
        Collection<SearchFilter> filters = SearchFilter.parse(params).values();
        Specification<Program> sp = SearchFilter.spec(filters, Program.class);
        return sp;
    }

   /* public List<Program> getList() {
        return dao.findByStatus(Program.NORMAL);
    }*/

    public Program get(Integer id) {
        return dao.findOne(id);
    }

  /*  public String replace(String s) {
        if (StringUtils.isBlank(s)) {
            return s;
        }
        for (Program word : getList()) {
            // 如果replacement为null，则会不替换，所以要转换成空字符串
            String replacement = word.getReplacement() != null ? word.getReplacement() : "";
            s = StringUtils.replace(s, word.getName(), replacement);
        }
        return s;
    }
*/
    @Transactional
    public Program save(Program bean, Integer siteId, User user) {
        bean.getName();
        bean = dao.save(bean);
        return bean;
    }

    @Transactional
    public Program update(Program bean) {
        bean.getName();
        bean = dao.save(bean);
        return bean;
    }

    @Transactional
    public Program delete(Integer id) {
        Program entity = dao.findOne(id);
        dao.delete(entity);
        return entity;
    }

    @Transactional
    public Program[] delete(Integer[] ids) {
        Program[] beans = new Program[ids.length];
        for (int i = 0; i < ids.length; i++) {
            beans[i] = delete(ids[i]);
        }
        return beans;
    }

    @Override
    public List<Program> findAll() {


        return dao.findAll();
    }

    private ProgramDao dao;

    @Autowired
    public void setDao(ProgramDao dao) {
        this.dao = dao;
    }

}
